package xcmg.device.web.announcement;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.announcement.BasicAttachmentService;
import xcmg.device.service.vo.announcement.BasicAttachmentVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/basicAttachment")
public class BasicAttachmentController extends BasicController {

    @Autowired
    private BasicAttachmentService basicAttachmentService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("附件信息查询")
    public ActionResult list(@RequestBody SearchCommonVO<BasicAttachmentVO> condition)throws Exception{
        BasicAttachmentVO basicAttachmentVO = condition.getFilters();
        if(basicAttachmentVO == null){
            basicAttachmentVO = new BasicAttachmentVO();
        }
        //根据ANNOUNCEMENT_ID查询附件信息
        condition.setFilters(basicAttachmentVO);
        PageCommonVO pageCommonVO = basicAttachmentService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("附件信息添加")
    public ActionResult addAnnouncement(@RequestBody List<BasicAttachmentVO> basicAttachmentVOList)throws Exception{
        if(null == basicAttachmentVOList || basicAttachmentVOList.isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        String userId = getUserId();
        List<BasicAttachmentVO> volist = new ArrayList<>();
        for (BasicAttachmentVO basicAttachmentVO:basicAttachmentVOList ) {
            basicAttachmentVO.setCreateId(userId);
            basicAttachmentVO.setCreateName(getUserName());
            basicAttachmentVO.setCreateDate(new Date());
            basicAttachmentVO.setUpdateId(userId);
            basicAttachmentVO.setUpdateDate(new Date());
            volist.add(basicAttachmentVO);
        }
        try {
            return actionResult(basicAttachmentService.add(volist));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.AnnouncementManage.Announcement_EDIT)
    @ApiOperation(value = "更新附件信息")
    public ActionResult update(@RequestBody BasicAttachmentVO model)throws Exception{
        if(model==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        model.setUpdateId(getUserId());
        model.setUpdateDate(new Date());
        ErrorCode ret=basicAttachmentService.update(model);
        return actionResult(ret);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "删除附件信息")
    public ActionResult remove(@RequestBody BasicAttachmentVO model){
        String[] ids = model.getIds();
        if(ids==null||ids.length==0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret=basicAttachmentService.remove(Arrays.asList(ids));
        return actionResult(ret);
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "附件信息单个查询")
    public ActionResult item(String id){
        if(StringUtils.isEmpty(id)){
            return actionResult(ErrorCode.IllegalArument);
        }
        BasicAttachmentVO result=basicAttachmentService.item(id);
        return actionValueResult(result);
    }
}
